/**
 * Copyright (C) 2011-2012 GreenSoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.com.greensoft.neosystem.util;

import ar.com.greensoft.neosystem.model.references.Sexo;

/**
 * 
 * @author Jorge E. Villaverde
 * @since 29/11/2012
 * @version 1.0.0
 */
public abstract class CUITGenerator {
	public static int calcularDigitoVerificador(String cuit) {
		int multiplicador = 2;
		int acumulado = 0;
		for(int i=9; i >= 0; i--){
			int posValue = Integer.parseInt(cuit.substring(i, i+1));
			acumulado += posValue*multiplicador;
			multiplicador++;
			if(multiplicador > 7)
				multiplicador = 2;
		}		
		int suma11 = 11 - (acumulado % 11);
		if(suma11 == 11)
			return 0;
		if(suma11 == 10)
			return 9;
		return suma11;
	}
	
	public static String genertePersonaFisica(Sexo sexo, Long numeroDocumento){
		StringBuilder builder = new StringBuilder();
		switch (sexo) {
		case MUJER:
			builder.append("27");
			break;
		case HOMBRE:
			builder.append("20");
			break;
		case OTRO:
			builder.append("23");
			break;
		default:
			break;
		}
		builder.append(String.format("%08d", numeroDocumento));
		builder.append(calcularDigitoVerificador(builder.toString()));
		return builder.toString();
	} 

	public static String genertePersonaJuridica(Long numeroDocumento){
		StringBuilder builder = new StringBuilder();
		builder.append("30");
		builder.append(String.format("%08d", numeroDocumento));
		builder.append(calcularDigitoVerificador(builder.toString()));
		return builder.toString();
	} 
	
}
